package com.dkx.wm.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dkx.wm.entity.User;
import com.dkx.wm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }

    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    public void addUser(User user) {
        userMapper.insert(user);
    }

    public void updateUser(User user) {
        userMapper.update(user);
    }

    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    public List<User> getUsersByPage(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return userMapper.selectByPage(offset, pageSize);
    }

    public int getTotalCount() {
        return userMapper.count();
    }

    /**
     * 分页查询用户
     * @param pageNow
     * @param pageSize
     * @return
     */
    public List<User> getUserInfoList(int pageNow, int pageSize) {
        Page<User> page = new Page<>(pageNow, pageSize);
        Page<User> userPage = userMapper.selectPage(page, null);
        return userPage.getRecords();
    }
}